/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.ui {
	import caurina.transitions.Tweener;
	
	import com.crl.core.IResizeable;
	import com.crl.managers.ResizeableManager;
	import com.crl.ui.controls.Component;
	import com.crlclr.*;
	import com.crlclr.events.*;
	import com.crlclr.managers.*;
	import com.crlclr.utils.QSize;
	import com.crlclr.utils.Sombra;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;

	public class LoaderBar extends Component implements IResizeable {
		private var bg:Shape;
		private var bar:Shape;
		private var loadText:TextField;
		private var size:QSize = new QSize(100, 50);
		private var barSize:QSize = new QSize(80, 4);
		
		private var startY:int = -200;
		private var isHide:Boolean = true;
		
		private static var instance:LoaderBar = new LoaderBar();
		public static function getInstance():LoaderBar {
			return instance;
		}
		
		public function LoaderBar() {
			if (instance != null){
				throw new Error("can't create Instance");
			} 
		}
		
		protected override function create():void {
			bg = new Shape();
			loadText = new TextField();
			QTextFormat.bindDefaultFiled(loadText);
			loadText.text = "Loading...";
			
			bar = new Shape();
			
			addChild(bg);
			addChild(bar);
			addChild(loadText);
		}
		
		override protected function draw():void {
			var g:Graphics = bg.graphics;
			g.beginFill(0xFFFFFF);
			var half:QSize = size.getHalf();
			g.drawRoundRect( -half.width, -half.height, size.width, size.height, 8, 8);
			
			
			g = bar.graphics;
			g.beginFill(0xff99cc);
			half = barSize.getHalf();
			g.drawRect(-half.width, -half.height-10, barSize.width, barSize.height)
			
			new Sombra(this.bg);
		}
		
		public function set progress(value:Number):void {
			this.loadText.text = "Loading " + int(value*100) + "%";
			var newXscale:Number = Math.min(1,value);
			Tweener.addTween(bar, { scaleX:newXscale, time:1 } );
			
			loadText.x = -loadText.textWidth / 2;
		}	
		public function show():void {
			isHide = false;
			progress =0;
			Tweener.addTween(this, { y:ResizeableManager.getInstance().stage.stageHeight>>1, time:1});
			
			ResizeableManager.getInstance().add(this);
		}
		
		public function hide():void {
			isHide = true;
			Tweener.addTween(this, { y:startY, time:1});
			
			ResizeableManager.getInstance().remove(this);
		}
		
		
		override public function resize(width:int,height:int):void{
			this.x = width>>1
			if(!isHide){
				this.y = height>>1
			}
		}
	}
	
}
